package com.kuaimacode.kframework.code.template;

import com.kuaimacode.kframework.code.domain.Field;
import com.kuaimacode.kframework.code.domain.Generate;
import com.kuaimacode.kframework.code.utils.CodeUtil;
import com.kuaimacode.kframework.code.utils.GenerateUtil;
import com.kuaimacode.kframework.code.utils.TemplateUtil;
import com.kuaimacode.kframework.util.ToolUtil;

import java.nio.file.FileAlreadyExistsException;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author Administrator
 * @date 2018/10/25
 */
public class DetailHtmlTemplate {

    private static final String trTab = CodeUtil.lineBreak + "            ";
    private static final String tdTab = CodeUtil.lineBreak + "                ";
    private static final String trLabel = "<div class=\"form-group\"><label class=\"col-sm-3 control-label\">%s：</label>";
    private static final String thLabel = "<th>%s</th>";
    private static final String tdLabel = "<div class=\"col-sm-9\">%s</div></div>";
    /**
     * 生成页面
     */
    private static String genHtmlBody(Generate generate) {
        // 构建数据
        String var = ToolUtil.lowerFirst(generate.getBasic().getTableEntity());
        String title = generate.getBasic().getGenTitle();
        String filePath = DetailHtmlTemplate.class.getResource("").getPath()
                + DetailHtmlTemplate.class.getSimpleName() + ".code";

        // 提取html页面
        String htmlTarget = TemplateUtil.getTemplate(filePath, "html");

        // 遍历字段
        StringBuilder fieldBuilder = new StringBuilder();
        StringBuilder tempBuilder = new StringBuilder();
        AtomicInteger count = new AtomicInteger();
        List<Field> fields = generate.getFields();
        final boolean[] fieldRemark = {false};
        fields.forEach(field -> {
            /*if(field.getName().equals("createBy") || field.getName().equals("createAt")){
                fieldBuilder.append(String.format(trTab + trLabel, tempBuilder + trTab));
                tempBuilder.setLength(0);
                count.set(0);
            }*/
            String text = "";
            switch (field.getName()) {
                /*case "createBy":
                case "updateBy":
                    break;*/
                case "createAt":
                case "updateAt":
                    text = "${(%s.%s?string('yyyy-MM-dd HH:mm:ss'))!}";
                    break;
//                case "remark":
//                    break;
                case "status":
                    text = "${convert('DATA_STATUS',%s.%s)!}";
                    break;
                default:
                    if(field.getDict() != null && !"".equals(field.getDict())){
                        text = "${convert('"+field.getDict()+"',%s.%s)!}";
                    }else{
                        text = "${%s.%s!}";
                    }

            }
            if (!text.isEmpty()) {
                //tempBuilder.append(String.format(tdTab + thLabel, field.getTitle()));
                tempBuilder.append(String.format(trLabel + tdLabel, field.getTitle(),String.format(text, var, field.getName()))).append(trTab);
            }
        });

//        // 判断是否需要remark字段
//        if (fieldRemark[0]){
//            tempBuilder.append(String.format(tdTab + thLabel, "备注"));
//            tempBuilder.append(String.format(tdTab + "<td th:text=\"${%s.remark}\" colspan=\"4\"></td>", var));
//            fieldBuilder.append(String.format(trTab + trLabel, tempBuilder + trTab));
//        }

        // 替换基本数据
        htmlTarget = htmlTarget.replace("#{field.detail}", tempBuilder );
        htmlTarget = htmlTarget.replace("#{var}", var );
        htmlTarget = htmlTarget.replace("#{title}", title );
        return htmlTarget;
    }

    /**
     * 生成列表页面模板
     */
    public static String generate(Generate generate) {
        String filePath = GenerateUtil.getHtmlFilePath(generate, "detail");
        String content = DetailHtmlTemplate.genHtmlBody(generate);
        try {
            GenerateUtil.generateFile(filePath, content);
        } catch (FileAlreadyExistsException e) {
            return GenerateUtil.fileExist(filePath);
        }
        return filePath;
    }
}
